#include "mainwindow.h"
#include "ui_mainwindow.h"

#include <QVideoWidget>
MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);

    player = new QMediaPlayer(this);
    //信息更新周期
    player->setNotifyInterval(2000);
    //设置视频显示组件
    player->setVideoOutput(ui->graphicsView);
    //设置显示组件的关联播放器
    ui->graphicsView->setMediaPlayer(player);

    connect(player,SIGNAL(stateChanged(QMediaPlayer::State)),this,SLOT(onStateButtonChanged(QMediaPlayer::State)));
    connect(player,SIGNAL(durationChanged(qint64)),this,SLOT(onDrtTimeChanged(qint64)));

    connect(player,SIGNAL(positionChanged(qint64)),this,SLOT(onPosTimeChanged(qint64)));

    connect(player, SIGNAL(errorOccurred(QMediaPlayer::Error)), this, SLOT(handleMediaError(QMediaPlayer::Error)));
    // 监听媒体状态变化
    connect(player, &QMediaPlayer::mediaStatusChanged, this, [](QMediaPlayer::MediaStatus status) {
        qDebug() << "Status changed:" << status;
    });
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::handleMediaError(QMediaPlayer::Error error) {
    qDebug() << "Error:" << player->errorString();
}


//改变按钮状态
void MainWindow::onStateButtonChanged(QMediaPlayer::State state)
{
    ui->pushButton_start->setEnabled(!(state == QMediaPlayer::PlayingState));
    ui->pushButton_pause->setEnabled(state == QMediaPlayer::PlayingState);
    ui->pushButton_stop->setEnabled(state == QMediaPlayer::PlayingState);

}

//视频文件时间更新变化
void MainWindow::onDrtTimeChanged(qint64 drt)
{


    qDebug() << "onDrtTimeChanged被调用...";
    ui->horizontalSlider_progress->setMaximum(drt);

    int sec = drt / 1000;
    int min = sec /60;
    sec = sec % 60;

    drtTime = QString::asprintf("%d:%d",min,sec);
    ui->label_time->setText(posTime + "|" + drtTime);
}

//播放视频当前的位置时间 更新变化
void MainWindow::onPosTimeChanged(qint64 pos)
{
    qDebug() << "onPosTimeChanged被调用...";
    if(ui->horizontalSlider_progress->isSliderDown())
    {
        return;
    }
    ui->horizontalSlider_progress->setSliderPosition(pos);

    int sec = pos / 1000;
    int min = sec / 60;
    sec = sec % 60;
    posTime = QString::asprintf("%d:%d",min,sec);
    ui->label_time->setText(posTime + "|" +drtTime);
}

//打开文件
void MainWindow::on_pushButton_open_clicked()
{
    //获取系统当前目录
    QString currentPath = QDir::homePath();
    QString dialogTitle = "请选择视频文件:";
    QString filter = "所有文件(*.*);;mp4文件(*.mp4)";
    QString strFile = QFileDialog::getOpenFileName(this,dialogTitle,currentPath,filter);

    if(strFile.isEmpty())
    {
        qDebug() << "strFile文件为空";
        return;
    }
    qDebug() << strFile;
    QFileInfo fileInfo(strFile);
    qDebug() << fileInfo.fileName();
    ui->label_videoName->setText(fileInfo.fileName());
    //设置播放文件
    player->setMedia(QUrl::fromLocalFile(strFile));
    qDebug() << "Media status:" << player->mediaStatus();
    qDebug() << "Error:" << player->errorString();
    //播放文件
    player->play();
}

//开始播放
void MainWindow::on_pushButton_start_clicked()
{
    player->play();
}

//暂停
void MainWindow::on_pushButton_pause_clicked()
{
    player->pause();
}

//停止
void MainWindow::on_pushButton_stop_clicked()
{
    player->stop();
}

//静音
void MainWindow::on_pushButton_volum_clicked()
{
    bool isMuted = player->isMuted();
    player->setMuted(!isMuted);
    if(!isMuted)
    {
        ui->pushButton_volum->setIcon(QIcon(":/images/mute.bmp"));
    }
    else
    {
        ui->pushButton_volum->setIcon(QIcon(":/images/volumn.bmp"));
    }

}

void MainWindow::on_horizontalSlider_progress_valueChanged(int value)
{
    player->setPosition(value);
}

void MainWindow::on_horizontalSlider_volum_valueChanged(int value)
{
    player->setVolume(value);
}
